package com.hequ.merchant.banner.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.hequ.merchant.banner.R;
import com.hequ.merchant.banner.entity.Banner;
import com.hequ.merchant.banner.util.NetworkImageUtil;
import com.hequ.merchant.banner.util.TimeUtil;

import java.util.List;

/**
 * Created by 柯尊诚 on 2015/10/20.
 * kzc
 */
public class BannerAdapter extends BaseAdapter{

    private Context context;
    private List<Banner> banners;

//    RequestQueue requestQueue;
//
    public BannerAdapter(Context context, List<Banner> banners) {
        this.context = context;
        this.banners = banners;
//        requestQueue = Volley.newRequestQueue(context);
//
//        //自动缓存处理
//        Glide.get(context).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(requestQueue));
//
    }

    @Override
    public int getCount() {
        return banners.size();
    }

    @Override
    public Banner getItem(int position) {
        return banners.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.banner_adapter, null);
            Button btn_down = (Button) convertView.findViewById(R.id.btn_down);

            btn_down.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null) {
                        listener.onStatusClick((Banner) v.getTag());
                    }
                }
            });
        }

        final Banner banner = getItem(position);

        TextView tv_title = (TextView) convertView.findViewById(R.id.tv_name);
        TextView tv_time = (TextView) convertView.findViewById(R.id.tv_time);
        Button btn_donw = (Button) convertView.findViewById(R.id.btn_down);
        NetworkImageView networkImageView = (NetworkImageView) convertView.findViewById(R.id.networkImageView);

        convertView.findViewById(R.id.btn_down).setTag(banner);

//        ImageLoader.getImageListener(networkImageView, R.drawable.news_pic_default1, R.drawable.news_pic_default1);
        tv_title.setText(banner.getName());
        tv_time.setText(TimeUtil.mm2time(banner.getUpdateTime()));
//        networkImageView.setImageUrl(banner.getImage(), showImageByNetworkImageView());
        NetworkImageUtil.getInstance(context).showImageByNetworkImageView(banner.getImage(), networkImageView);

        if (banner.getModules().size() == 0)
            btn_donw.setVisibility(View.INVISIBLE);
        btn_donw.getBackground().setAlpha(200);




        return convertView;
    }


//    private ImageLoader showImageByNetworkImageView() {
//        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);
//        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
//        return imageLoader;
//    }

    private OnBannerListener listener;

    public void setListener(OnBannerListener listener) {
        this.listener = listener;
    }

    public interface OnBannerListener {

        void onStatusClick(Banner banner);

    }

}
